package com.hnas.trade.dao.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.hnas.common.util.CommonUtils;
import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.trade.dao.TradeRecordDetailDao;
import com.hnas.trade.orm.TradeRecordDetail;

@Component("tradeRecordDetailDao")
public class TradeRecordDetailDaoImpl extends BaseDaoImpl<TradeRecordDetail> implements TradeRecordDetailDao{

	public TradeRecordDetailDaoImpl() {
		super(TradeRecordDetail.class);
	}

	@Override
	public List<TradeRecordDetail> findByOrderNo(String orderNo) {
		return this.findByQuery("from OrderDetail t where t.orderNo=?1", orderNo);
	}

	@Override
	public TradeRecordDetail save(TradeRecordDetail dto) throws Exception {
		dto.setId(CommonUtils.getUUID());
		dto.setCreateTime(DateUtil.dateTimeToString(new Date()));
		dto.setUpdateTime(DateUtil.dateTimeToString(new Date()));
		this.persist(dto);
		return dto;
	}

	@Override
	public List<TradeRecordDetail> getByOrderNoAndType(String orderNo, String key) {
		if(StringUtil.isEmpty(orderNo)||StringUtil.isEmpty(key)){
			return null;
		}
		String hql = "from TradeRecordDetail t where t.orderNo = ?1 and t.type=?2";
		Query query = this.entityManager.createQuery(hql);
		query.setParameter(1, orderNo);
		query.setParameter(2, key);
		return query.getResultList();
	}

	
}
